package com.zhengshang.common.utils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    //跟Calendar中数值保持一致
    public enum DateType {
        ERA(Calendar.ERA), YEAR(Calendar.YEAR), MONTH(Calendar.MONTH), WEEK_OF_YEAR(Calendar.WEEK_OF_YEAR), WEEK_OF_MONTH(Calendar.WEEK_OF_MONTH), DATE(Calendar.DATE),
        DAY_OF_MONTH(Calendar.DAY_OF_MONTH), DAY_OF_YEAR(Calendar.DAY_OF_YEAR), DAY_OF_WEEK(Calendar.DAY_OF_WEEK), DAY_OF_WEEK_IN_MONTH(Calendar.DAY_OF_WEEK_IN_MONTH),
        AM_PM(Calendar.AM_PM), HOUR(Calendar.HOUR), HOUR_OF_DAY(Calendar.HOUR_OF_DAY), MINUTE(Calendar.MINUTE), SECOND(Calendar.SECOND), MILLISECOND(Calendar.MILLISECOND),
        ZONE_OFFSET(Calendar.ZONE_OFFSET), DST_OFFSET(Calendar.DST_OFFSET), FIELD_COUNT(Calendar.FIELD_COUNT);
        private int type;

        DateType(int dateType) {
            this.type = dateType;
        }

        public int getType() {
            return this.type;
        }

    }

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    /**
     * 获取昨天日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getYesterDay() {
        return dateYesterDay(YYYY_MM_DD);
    }

    private static String dateYesterDay(final String format) {
        Date date = addDate(getNowDate(), -1);
        return parseDateToStr(format, date);
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM
     *
     * @return String
     */
    public static String getMonth() {
        return dateTimeNow(YYYY_MM);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }


    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String getDateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, YYYY_MM_DD_HH_MM_SS);
    }

    /**
     * 日期推算
     *
     * @param days    推算的天数
     * @param pattern 日期格式
     * @return 结果
     */
    public static String dateCalculationDay(int days, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + days);
        return DateFormatUtils.format(calendar.getTime(), pattern);
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 日期推算
     *
     * @param type
     * @param date 要推算的日期
     * @param num  要推算的数值（如果往前推算，填入负数即可）
     * @return
     */
    public static long calculationDate(DateType type, Date date, int num) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(type.getType(), now.get(type.getType()) + num);
        return now.getTimeInMillis();
    }

    /**
     * 日期推算
     *
     * @param type
     * @param date   要推算的日期
     * @param num    要推算的数值（如果往前推算，填入负数即可）
     * @param format 日期格式
     * @return
     */
    public static String calculationDate(DateType type, Date date, int num, String format) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(type.getType(), now.get(type.getType()) + num);
        return parseDateToStr(format, now.getTime());
    }

    /**
     * 日期推算分钟
     *
     * @param date
     * @param min
     * @return
     */
    public static Date addMin(Date date, int min) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(Calendar.MINUTE, min);
        date = calendar.getTime();
        return date;
    }

    /**
     * 日期推算分钟
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); //需要将date数据转移到Calender对象中操作
        calendar.add(Calendar.DATE, days);
        date = calendar.getTime();
        return date;
    }

    public static void main(String[] agrs) {
        String beginDate = DateUtils.getDate() + " " + "10:00";
        String endDate = DateUtils.getDate() + " " + "23:59";
        Date now = DateUtils.getNowDate();
        System.out.println(dateTimeNow("DD"));
        if (now.after(DateUtils.parseDate(endDate))) {
            System.out.println("32223");
        }
    }
}
